home *** CD-ROM | disk | FTP | other *** search
/ CD Actual Thematic 7: Programming / CDAT7.iso / demos / VisualAge for Java 2.0 Entry / setup / data1.cab / ide-e / IDE / cache / 1SDVH34 (.txt) < prev    next >
Encoding:
Java Class File  |  1998-09-16  |  9.0 KB  |  407 lines

  1. package com.sun.java.swing;
  2.  
  3. import com.sun.java.swing.border.Border;
  4. import java.awt.Component;
  5. import java.awt.Container;
  6. import java.awt.Dimension;
  7. import java.awt.Insets;
  8. import java.awt.LayoutManager;
  9. import java.awt.Rectangle;
  10. import java.io.Serializable;
  11.  
  12. public class ScrollPaneLayout implements LayoutManager, ScrollPaneConstants, Serializable {
  13.    protected JViewport viewport;
  14.    protected JScrollBar vsb;
  15.    protected JScrollBar hsb;
  16.    protected JViewport rowHead;
  17.    protected JViewport colHead;
  18.    protected Component lowerLeft;
  19.    protected Component lowerRight;
  20.    protected Component upperLeft;
  21.    protected Component upperRight;
  22.    protected int vsbPolicy = 20;
  23.    protected int hsbPolicy = 30;
  24.  
  25.    public void addLayoutComponent(String s, Component c) {
  26.       if (s.equals("VIEWPORT")) {
  27.          this.viewport = (JViewport)this.addSingletonComponent(this.viewport, c);
  28.       } else if (s.equals("VERTICAL_SCROLLBAR")) {
  29.          this.vsb = (JScrollBar)this.addSingletonComponent(this.vsb, c);
  30.       } else if (s.equals("HORIZONTAL_SCROLLBAR")) {
  31.          this.hsb = (JScrollBar)this.addSingletonComponent(this.hsb, c);
  32.       } else if (s.equals("ROW_HEADER")) {
  33.          this.rowHead = (JViewport)this.addSingletonComponent(this.rowHead, c);
  34.       } else if (s.equals("COLUMN_HEADER")) {
  35.          this.colHead = (JViewport)this.addSingletonComponent(this.colHead, c);
  36.       } else if (s.equals("LOWER_LEFT_CORNER")) {
  37.          this.lowerLeft = this.addSingletonComponent(this.lowerLeft, c);
  38.       } else if (s.equals("LOWER_RIGHT_CORNER")) {
  39.          this.lowerRight = this.addSingletonComponent(this.lowerRight, c);
  40.       } else if (s.equals("UPPER_LEFT_CORNER")) {
  41.          this.upperLeft = this.addSingletonComponent(this.upperLeft, c);
  42.       } else {
  43.          if (!s.equals("UPPER_RIGHT_CORNER")) {
  44.             throw new IllegalArgumentException("invalid layout key " + s);
  45.          }
  46.  
  47.          this.upperRight = this.addSingletonComponent(this.upperRight, c);
  48.       }
  49.  
  50.    }
  51.  
  52.    protected Component addSingletonComponent(Component oldC, Component newC) {
  53.       if (oldC != null && oldC != newC) {
  54.          oldC.getParent().remove(oldC);
  55.       }
  56.  
  57.       return newC;
  58.    }
  59.  
  60.    public JViewport getColumnHeader() {
  61.       return this.colHead;
  62.    }
  63.  
  64.    public Component getCorner(String key) {
  65.       if (key.equals("LOWER_LEFT_CORNER")) {
  66.          return this.lowerLeft;
  67.       } else if (key.equals("LOWER_RIGHT_CORNER")) {
  68.          return this.lowerRight;
  69.       } else if (key.equals("UPPER_LEFT_CORNER")) {
  70.          return this.upperLeft;
  71.       } else {
  72.          return key.equals("UPPER_RIGHT_CORNER") ? this.upperRight : null;
  73.       }
  74.    }
  75.  
  76.    public JScrollBar getHorizontalScrollBar() {
  77.       return this.hsb;
  78.    }
  79.  
  80.    public int getHorizontalScrollBarPolicy() {
  81.       return this.hsbPolicy;
  82.    }
  83.  
  84.    public JViewport getRowHeader() {
  85.       return this.rowHead;
  86.    }
  87.  
  88.    public JScrollBar getVerticalScrollBar() {
  89.       return this.vsb;
  90.    }
  91.  
  92.    public int getVerticalScrollBarPolicy() {
  93.       return this.vsbPolicy;
  94.    }
  95.  
  96.    public JViewport getViewport() {
  97.       return this.viewport;
  98.    }
  99.  
  100.    public Rectangle getViewportBorderBounds(JScrollPane sp) {
  101.       Rectangle borderR = new Rectangle(((Component)sp).getSize());
  102.       Insets insets = ((JComponent)sp).getInsets();
  103.       borderR.x = insets.left;
  104.       borderR.y = insets.top;
  105.       borderR.width -= insets.left + insets.right;
  106.       borderR.height -= insets.top + insets.bottom;
  107.       if (this.colHead != null && this.colHead.isVisible()) {
  108.          int colHeadHeight = this.colHead.getHeight();
  109.          borderR.y += colHeadHeight;
  110.          borderR.height -= colHeadHeight;
  111.       }
  112.  
  113.       if (this.rowHead != null && this.rowHead.isVisible()) {
  114.          int rowHeadWidth = this.rowHead.getWidth();
  115.          borderR.x += rowHeadWidth;
  116.          borderR.width -= rowHeadWidth;
  117.       }
  118.  
  119.       if (this.vsb != null && this.vsb.isVisible()) {
  120.          borderR.width -= this.vsb.getWidth();
  121.       }
  122.  
  123.       if (this.hsb != null && this.hsb.isVisible()) {
  124.          borderR.height -= this.hsb.getHeight();
  125.       }
  126.  
  127.       return borderR;
  128.    }
  129.  
  130.    public void layoutContainer(Container parent) {
  131.       Rectangle availR = new Rectangle(((Component)parent).getSize());
  132.       Insets insets = parent.getInsets();
  133.       availR.x = insets.left;
  134.       availR.y = insets.top;
  135.       availR.width -= insets.left + insets.right;
  136.       availR.height -= insets.top + insets.bottom;
  137.       Rectangle colHeadR = new Rectangle(0, availR.y, 0, 0);
  138.       if (this.colHead != null && this.colHead.isVisible()) {
  139.          int colHeadHeight = this.colHead.getPreferredSize().height;
  140.          colHeadR.height = colHeadHeight;
  141.          availR.y += colHeadHeight;
  142.          availR.height -= colHeadHeight;
  143.       }
  144.  
  145.       Rectangle rowHeadR = new Rectangle(availR.x, 0, 0, 0);
  146.       if (this.rowHead != null && this.rowHead.isVisible()) {
  147.          int rowHeadWidth = this.rowHead.getPreferredSize().width;
  148.          rowHeadR.width = rowHeadWidth;
  149.          availR.x += rowHeadWidth;
  150.          availR.width -= rowHeadWidth;
  151.       }
  152.  
  153.       Border viewportBorder = ((JScrollPane)parent).getViewportBorder();
  154.       Insets vpbInsets;
  155.       if (viewportBorder != null) {
  156.          vpbInsets = viewportBorder.getBorderInsets(parent);
  157.          availR.x += vpbInsets.left;
  158.          availR.y += vpbInsets.top;
  159.          availR.width -= vpbInsets.left + vpbInsets.right;
  160.          availR.height -= vpbInsets.top + vpbInsets.bottom;
  161.       } else {
  162.          vpbInsets = new Insets(0, 0, 0, 0);
  163.       }
  164.  
  165.       colHeadR.x = availR.x;
  166.       rowHeadR.y = availR.y;
  167.       Component view = this.viewport != null ? this.viewport.getView() : null;
  168.       Dimension viewSize = view != null ? view.getPreferredSize() : new Dimension(0, 0);
  169.       Dimension extentSize = this.viewport != null ? this.viewport.toViewCoordinates(availR.getSize()) : new Dimension(0, 0);
  170.       Rectangle vsbR = new Rectangle(0, availR.y - vpbInsets.top, 0, 0);
  171.       boolean vsbNeeded = this.vsbPolicy == 22 || viewSize.height > extentSize.height && this.vsbPolicy == 20;
  172.       if (this.vsb != null && vsbNeeded) {
  173.          int vsbWidth = this.vsb.getPreferredSize().width;
  174.          availR.width -= vsbWidth;
  175.          vsbR.x = availR.x + availR.width + vpbInsets.right;
  176.          vsbR.width = vsbWidth;
  177.       }
  178.  
  179.       Rectangle hsbR = new Rectangle(availR.x - vpbInsets.left, 0, 0, 0);
  180.       boolean hsbNeeded = this.hsbPolicy == 32 || viewSize.width > extentSize.width && this.hsbPolicy == 30;
  181.       if (this.hsb != null && hsbNeeded) {
  182.          int hsbHeight = this.hsb.getPreferredSize().height;
  183.          availR.height -= hsbHeight;
  184.          hsbR.y = availR.y + availR.height + vpbInsets.bottom;
  185.          hsbR.height = hsbHeight;
  186.          if (this.vsb != null && !vsbNeeded && this.vsbPolicy != 21) {
  187.             extentSize = this.viewport.toViewCoordinates(availR.getSize());
  188.             vsbNeeded = viewSize.height > extentSize.height;
  189.             if (vsbNeeded) {
  190.                int vsbWidth = this.vsb.getPreferredSize().width;
  191.                availR.width -= vsbWidth;
  192.                vsbR.x = availR.x + availR.width + vpbInsets.right;
  193.                vsbR.width = vsbWidth;
  194.             }
  195.          }
  196.       }
  197.  
  198.       vsbR.height = availR.height + vpbInsets.top + vpbInsets.bottom;
  199.       hsbR.width = availR.width + vpbInsets.left + vpbInsets.right;
  200.       rowHeadR.height = availR.height;
  201.       colHeadR.width = availR.width;
  202.       if (this.viewport != null) {
  203.          this.viewport.setBounds(availR);
  204.       }
  205.  
  206.       if (this.rowHead != null) {
  207.          this.rowHead.setBounds(rowHeadR);
  208.       }
  209.  
  210.       if (this.colHead != null) {
  211.          this.colHead.setBounds(colHeadR);
  212.       }
  213.  
  214.       if (this.vsb != null) {
  215.          if (vsbNeeded) {
  216.             this.vsb.setVisible(true);
  217.             this.vsb.setBounds(vsbR);
  218.          } else {
  219.             this.vsb.setVisible(false);
  220.          }
  221.       }
  222.  
  223.       if (this.hsb != null) {
  224.          if (hsbNeeded) {
  225.             this.hsb.setVisible(true);
  226.             this.hsb.setBounds(hsbR);
  227.          } else {
  228.             this.hsb.setVisible(false);
  229.          }
  230.       }
  231.  
  232.       if (this.lowerLeft != null) {
  233.          this.lowerLeft.setBounds(rowHeadR.x, hsbR.y, rowHeadR.width, hsbR.height);
  234.       }
  235.  
  236.       if (this.lowerRight != null) {
  237.          this.lowerRight.setBounds(vsbR.x, hsbR.y, vsbR.width, hsbR.height);
  238.       }
  239.  
  240.       if (this.upperLeft != null) {
  241.          this.upperLeft.setBounds(rowHeadR.x, colHeadR.y, rowHeadR.width, colHeadR.height);
  242.       }
  243.  
  244.       if (this.upperRight != null) {
  245.          this.upperRight.setBounds(vsbR.x, colHeadR.y, vsbR.width, colHeadR.height);
  246.       }
  247.  
  248.    }
  249.  
  250.    public Dimension minimumLayoutSize(Container parent) {
  251.       Insets insets = parent.getInsets();
  252.       int minWidth = insets.left + insets.right;
  253.       int minHeight = insets.top + insets.bottom;
  254.       if (this.viewport != null) {
  255.          Dimension size = this.viewport.getMinimumSize();
  256.          minWidth += size.width;
  257.          minHeight += size.height;
  258.       }
  259.  
  260.       Border viewportBorder = ((JScrollPane)parent).getViewportBorder();
  261.       if (viewportBorder != null) {
  262.          Insets vpbInsets = viewportBorder.getBorderInsets(parent);
  263.          minWidth += vpbInsets.left + vpbInsets.right;
  264.          minHeight += vpbInsets.top + vpbInsets.bottom;
  265.       }
  266.  
  267.       if (this.rowHead != null && this.rowHead.isVisible()) {
  268.          Dimension size = this.rowHead.getMinimumSize();
  269.          minWidth += size.width;
  270.          minHeight = Math.max(minHeight, size.height);
  271.       }
  272.  
  273.       if (this.colHead != null && this.colHead.isVisible()) {
  274.          Dimension size = this.colHead.getMinimumSize();
  275.          minWidth = Math.max(minWidth, size.width);
  276.          minHeight += size.height;
  277.       }
  278.  
  279.       if (this.vsb != null && this.vsbPolicy != 21) {
  280.          Dimension size = this.vsb.getMinimumSize();
  281.          minWidth += size.width;
  282.          minHeight = Math.max(minHeight, size.height);
  283.       }
  284.  
  285.       if (this.hsb != null && this.hsbPolicy != 21) {
  286.          Dimension size = this.hsb.getMinimumSize();
  287.          minWidth = Math.max(minWidth, size.width);
  288.          minHeight += size.height;
  289.       }
  290.  
  291.       return new Dimension(minWidth, minHeight);
  292.    }
  293.  
  294.    public Dimension preferredLayoutSize(Container parent) {
  295.       Insets insets = parent.getInsets();
  296.       int prefWidth = insets.left + insets.right;
  297.       int prefHeight = insets.top + insets.bottom;
  298.       Dimension extentSize = null;
  299.       Dimension viewSize = null;
  300.       Component view = null;
  301.       if (this.viewport != null) {
  302.          extentSize = this.viewport.getPreferredSize();
  303.          viewSize = this.viewport.getViewSize();
  304.          view = this.viewport.getView();
  305.       }
  306.  
  307.       if (extentSize != null) {
  308.          prefWidth += extentSize.width;
  309.          prefHeight += extentSize.height;
  310.       }
  311.  
  312.       Border viewportBorder = ((JScrollPane)parent).getViewportBorder();
  313.       if (viewportBorder != null) {
  314.          Insets vpbInsets = viewportBorder.getBorderInsets(parent);
  315.          prefWidth += vpbInsets.left + vpbInsets.right;
  316.          prefHeight += vpbInsets.top + vpbInsets.bottom;
  317.       }
  318.  
  319.       if (this.rowHead != null && this.rowHead.isVisible()) {
  320.          prefWidth += this.rowHead.getPreferredSize().width;
  321.       }
  322.  
  323.       if (this.colHead != null && this.colHead.isVisible()) {
  324.          prefHeight += this.colHead.getPreferredSize().height;
  325.       }
  326.  
  327.       if (this.vsb != null && this.vsbPolicy != 21) {
  328.          if (this.vsbPolicy == 22) {
  329.             prefWidth += this.vsb.getPreferredSize().width;
  330.          } else if (viewSize != null && extentSize != null) {
  331.             boolean canScroll = true;
  332.             if (view instanceof Scrollable) {
  333.                canScroll = !((Scrollable)view).getScrollableTracksViewportHeight();
  334.             }
  335.  
  336.             if (canScroll && viewSize.height > extentSize.height) {
  337.                prefWidth += this.vsb.getPreferredSize().width;
  338.             }
  339.          }
  340.       }
  341.  
  342.       if (this.hsb != null && this.hsbPolicy != 31) {
  343.          if (this.hsbPolicy == 32) {
  344.             prefHeight += this.hsb.getPreferredSize().height;
  345.          } else if (viewSize != null && extentSize != null) {
  346.             boolean canScroll = true;
  347.             if (view instanceof Scrollable) {
  348.                canScroll = !((Scrollable)view).getScrollableTracksViewportWidth();
  349.             }
  350.  
  351.             if (canScroll && viewSize.width > extentSize.width) {
  352.                prefHeight += this.hsb.getPreferredSize().height;
  353.             }
  354.          }
  355.       }
  356.  
  357.       return new Dimension(prefWidth, prefHeight);
  358.    }
  359.  
  360.    public void removeLayoutComponent(Component c) {
  361.       if (c == this.viewport) {
  362.          this.viewport = null;
  363.       } else if (c == this.vsb) {
  364.          this.vsb = null;
  365.       } else if (c == this.hsb) {
  366.          this.hsb = null;
  367.       } else if (c == this.rowHead) {
  368.          this.rowHead = null;
  369.       } else if (c == this.colHead) {
  370.          this.colHead = null;
  371.       } else if (c == this.lowerLeft) {
  372.          this.lowerLeft = null;
  373.       } else if (c == this.lowerRight) {
  374.          this.lowerLeft = null;
  375.       } else if (c == this.upperLeft) {
  376.          this.upperLeft = null;
  377.       } else if (c == this.upperRight) {
  378.          this.upperRight = null;
  379.       }
  380.  
  381.    }
  382.  
  383.    public void setHorizontalScrollBarPolicy(int x) {
  384.       switch (x) {
  385.          case 30:
  386.          case 31:
  387.          case 32:
  388.             this.hsbPolicy = x;
  389.             return;
  390.          default:
  391.             throw new IllegalArgumentException("invalid horizontalScrollBarPolicy");
  392.       }
  393.    }
  394.  
  395.    public void setVerticalScrollBarPolicy(int x) {
  396.       switch (x) {
  397.          case 20:
  398.          case 21:
  399.          case 22:
  400.             this.vsbPolicy = x;
  401.             return;
  402.          default:
  403.             throw new IllegalArgumentException("invalid verticalScrollBarPolicy");
  404.       }
  405.    }
  406. }
  407.